from django.test import TestCase
from user.models import User
from django.urls import reverse
from django.utils import timezone


def update_last_login_time():
    time = timezone.now()
    user = User.objects.get(username='hahahahah')
    user.last_login = time
    user.save()


class MyTest(TestCase):

    def setUp(self) -> None:
        User.objects.create(username='hahahahah', password='12345678')

    def test_create(self):
        User.objects.create(username='liu', password='12345678')

        c = User.objects.get(username='liu')
        self.assertEqual(c.username, 'liu')
        self.assertEqual(c.password, '12345678')

    def test_delete(self):
        c = User.objects.get(username='hahahahah')
        c.delete()
        ret = User.objects.filter(username='hahahahah')
        self.assertEqual(len(ret), 0)

    def test_response(self):
        response = self.client.get(reverse('users:index'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "An example of Django")
        # 将对象转化为供解释器读取的形式，解析response.context['DEFAULT_MESSAGE_LEVELS']的值
        self.assertQuerysetEqual(response.context['DEFAULT_MESSAGE_LEVELS'],
                                 ["'DEBUG'", "'INFO'", "'SUCCESS'", "'WARNING'", "'ERROR'"])

    def test_past_question(self):
        update_last_login_time()
        user = User.objects.get(username='hahahahah')

        self.assertEqual(
            user.last_login.strftime('%T-%m-%d %H:%M:%S'),
            timezone.now().strftime('%T-%m-%d %H:%M:%S'),
        )

    # def test_future_question(self):
    #     """
    #     Questions with a pub_date in the future aren't displayed on
    #     the index page.
    #     """
    #     create_question(question_text="Future question.", days=30)
    #     response = self.client.get(reverse('polls:index'))
    #     self.assertContains(response, "No polls are available.")
    #     self.assertQuerysetEqual(response.context['latest_question_list'], [])
    #
    # def test_future_question_and_past_question(self):
    #     """
    #     Even if both past and future questions exist, only past questions
    #     are displayed.
    #     """
    #     question = create_question(question_text="Past question.", days=-30)
    #     create_question(question_text="Future question.", days=30)
    #     response = self.client.get(reverse('polls:index'))
    #     self.assertQuerysetEqual(
    #         response.context['latest_question_list'],
    #         [question],
    #     )
    #
    # def test_two_past_questions(self):
    #     """
    #     The questions index page may display multiple questions.
    #     """
    #     question1 = create_question(question_text="Past question 1.", days=-30)
    #     question2 = create_question(question_text="Past question 2.", days=-5)
    #     response = self.client.get(reverse('polls:index'))
    #     self.assertQuerysetEqual(
    #         response.context['latest_question_list'],
    #         [question2, question1],
    #     )

